package com.example.myapplication.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.bean.NewsBean;

import java.util.List;

public class NewsAdapter extends BaseAdapter {//线性布局适配器
    Context context;//上下文（必写）
    List<NewsBean.ResultBean.DataBean> resultBeanList;

    public NewsAdapter(Context context, List<NewsBean.ResultBean.DataBean> resultBeanList) {//Alt+insert按键导入
        this.context = context;
        this.resultBeanList = resultBeanList;
    }

    @Override
    public int getCount() {
        return resultBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return resultBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = View.inflate(context, R.layout.news_item_layout,null);
        }
        ImageView newsPicTv = convertView.findViewById(R.id.news_pic_iv);
        TextView newsTitleTv = convertView.findViewById(R.id.news_title_tv);
        TextView newsTimeTv = convertView.findViewById(R.id.news_time_tv);
        newsTitleTv.setText(resultBeanList.get(position).getTitle()+"");//标题
        newsTimeTv.setText(resultBeanList.get(position).getDate()+"");//时间
        Glide.with(context).load(resultBeanList.get(position).getThumbnail_pic_s()).into(newsPicTv);//加载网络图片资源
        return convertView;
    }
}
